package com.gc.ip;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.beans.PropertyChangeListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;


@SuppressWarnings("serial")
public class JIPAddress extends JPanel implements IPEditor
{
	private IPEditor ipEditor = null;
	private IPIndicator ipIndicator = null;
	
	public JIPAddress(IPEditor ipEditor, IPIndicator ipIndicator)
	{
		this.ipEditor = ipEditor;
		this.ipIndicator = ipIndicator;
		
		initGUI();
		addListeners();
	}

	public JIPAddress(boolean hasPrefixLength, IPType ipType)
	{
		ipEditor = new JIPAnyEditor(hasPrefixLength, ipType);
		ipIndicator = new JLabelIPIndicator(ipEditor.getIPAddress() == null ? null : ipEditor
			.getIPAddress().getIPType());

		initGUI();
		addListeners();
	}
	public JIPAddress()
	{
		this(false, IPType.IPv4_v6);
	}

	private void initGUI()
	{
		this.setLayout(new BorderLayout());
		this.add(ipEditor.getUiComponent(), BorderLayout.CENTER);
		if(ipIndicator != null)
		{
			this.add(ipIndicator.getUiComponent(), BorderLayout.EAST);
			
			Insets editorBorderInsets = null;
			Border editorBorder = ipEditor.getUiComponent().getBorder();
			if(editorBorder != null)
			{
				editorBorderInsets = editorBorder.getBorderInsets(ipEditor.getUiComponent());
			}
			if(editorBorderInsets == null)
			{
				editorBorderInsets = new Insets(2, 2, 2, 2);
			}
			
			//remove the ipEditor's border
			ipEditor.getUiComponent().setBorder(new EmptyBorder(editorBorderInsets));
			
			//add a whole border as one part
			if(editorBorder == null)
			{
				editorBorder = new LineBorder(new Color(127, 157, 185));
			}
			//This makes the editor and indicator looks like in one component
			this.setBorder(editorBorder);
		}
	}

	private void addListeners()
	{
		addIPEditorListener(IPEditor.IP_TYPE_PROPERTY);
		addIPEditorListener(IPEditor.IP_ADDRESS_PROPERTY);
		addIPEditorListener(IPEditor.TEXT_PROPERTY);
	}
	
	private void addIPEditorListener(String propertyName)
	{
		if(ipIndicator != null)
		{
			ipEditor.addPropertyChangeListener(propertyName, ipIndicator.getChangeListener(propertyName));
		}
	}
	
	/**
	 * @return
	 * @see com.gc.ip.IPEditor#getIPAddress()
	 */
	public IPAddress getIPAddress()
	{
		return ipEditor.getIPAddress();
	}
	/**
	 * @param ipAddress
	 * @see com.gc.ip.IPEditor#setIPAddress(com.gc.ip.IPAddress)
	 */
	public void setIPAddress(IPAddress ipAddress)
	{
		ipEditor.setIPAddress(ipAddress);
	}
	
	@Override
	public JComponent getUiComponent()
	{
		return this;
	}
	
	@Override
	public void setEnabled(boolean enabled)
	{
		super.setEnabled(enabled);
		ipEditor.getUiComponent().setEnabled(enabled);
	}
	public void addPropertyChangeListener(PropertyChangeListener listener)
	{
		super.addPropertyChangeListener(listener);
		if(ipEditor != null)
		{
			ipEditor.addPropertyChangeListener(listener);
		}
	}
	public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
	{
		super.addPropertyChangeListener(propertyName, listener);
		if(ipEditor != null)
		{
			ipEditor.addPropertyChangeListener(propertyName, listener);
		}
	}
	public void removePropertyChangeListener(PropertyChangeListener listener)
	{
		super.removePropertyChangeListener(listener);
		if(ipEditor != null)
		{
			ipEditor.removePropertyChangeListener(listener);
		}
	}
	public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
	{
		super.removePropertyChangeListener(propertyName, listener);
		if(ipEditor != null)
		{
			ipEditor.removePropertyChangeListener(propertyName, listener);
		}
	}
	
	public static void main(String[] args)
	{
		JFrame frame = new JFrame("JIPAddress Test");
		
		//Create a JIPAddress instance. It is actually a JPanel
		//The component has PrefixLength and allows both IPv4 and IPv6
		JIPAddress jIpAddress = new JIPAddress(true, IPType.IPv4_v6);
		
		//Create model data: IPv4 address with subnet mask
		IPAddress ip = new IPAddress("192.168.0.183", "255.255.0.0");
		//Initialize the JIPAddress component with some data
		jIpAddress.setIPAddress(ip);
		
		//GUI related operations
		frame.getContentPane().add(jIpAddress);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(500, 60));
        frame.pack();
        frame.setVisible(true);
	}
}
